Ext.define('Admin.view.emergencyDispatch.DdsEdPController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.ddsEdPController',

    init: function() {

    },

    autoLoad: true,

    //应急事件查询
    onSearchClick: function() {
        var ddsEdPGrid = this.lookupReference('ddsEdPGrid');
        var store = ddsEdPGrid.getStore();
        var evenTp = this.lookupReference('evenTp').getValue();
        // var loc = this.lookupReference('loc').getValue();
        var startTime = this.lookupReference('startTime').getValue();
        var endTime = this.lookupReference('endTime').getValue();
        if (startTime > endTime) {
            Ext.Msg.alert('提示', '起始时间需要小于结束时间'); //这种方式非常常见的
            return;
        }
        store.getProxy().extraParams = {
            evenTp: evenTp,
            // loc: loc,
            river: config.RIVER,
            startTime: Ext.Date.format(startTime, 'Y-m-d'),
            endTime: Ext.Date.format(endTime, 'Y-m-d')
        };
        store.reload();
    },

    //删除动作
    onDelClick: function(grid, rowIndex, colIndex) {
        var proCd = grid.getStore().getAt(rowIndex).get('proCd');
        Ext.Msg.confirm("警告", "确定要删除吗？", function(button) {
            if (button === 'yes') {
                Ext.Ajax.request({
                    url: config.URL + 'emergency/ddsEdP/delete',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        if (response.responseText) {
                            grid.store.load();
                            Ext.Msg.alert('系统提示', "删除成功");
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: { proCd: proCd, river: config.RIVER }
                });
            }
        });

    },

    //新增
    onAddClick: function() {
        var _that = this;
        var win = _that.lookupReference('warnAddWindow');
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '新增应急方案',
                width: 530,
                autoScroll: true,
                closeAction: 'hide',
                resizable: false,
                reference: 'warnAddWindow',
                items: {
                    xtype: 'warnForm',
                    border: false,
                    reference: 'warnAddForm'
                },
                buttons: [{
                        text: '保存',
                        handler: function() {
                            var form = _that.lookupReference('warnAddForm').getForm();
                            if (form.isValid()) {
                                var modal = this.up('window');
                                var meeetingGrid = _that.lookupReference('ddsEdPGrid');
                                var store = meeetingGrid.getStore();
                                var values = form.getValues();
                                if (values.bgDt > values.edDt) {
                                    Ext.Msg.alert('提示', '起始时间需要小于结束时间'); //这种方式非常常见的
                                    return;
                                }
                                values.river = config.RIVER;
                                Ext.Ajax.request({
                                    url: config.URL + 'emergency/ddsEdP/save',
                                    method: "post",
                                    success: function(response, opts) {
                                        var result = Ext.JSON.decode(response.responseText);
                                        Ext.MessageBox.hide();
                                        if (result.code === 1) {
                                            Ext.Msg.alert('系统提示', "保存成功");
                                            store.reload();
                                            modal.hide();
                                        } else {
                                            Ext.Msg.alert("系统提示", result.msg);
                                        }
                                    },
                                    failure: function() {
                                        Ext.Msg.alert('系统提示', '系统出错！');
                                    },
                                    params: values
                                });
                            } else {
                                Ext.Msg.alert('提示', '表单填写不合法！');
                            }

                        }
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            modal.hide();
                        }
                    },
                ]

            });
            this.getView().add(win);
            // Ext.ComponentQuery.query('#mainView')[0].add(win);
        }
        win.show();
    },

    //修改应急方案管理
    onUpdataClick: function(grid, rowIndex, colIndex) {
        var rec = grid.getStore().getAt(rowIndex);

        var _that = this;
        var win = this.lookupReference('warnUpdataWindow');
        var saveUpdata = function() {
            var form = _that.lookupReference('warnUpdataForm').getForm();
            if (form.isValid()) {
                var modal = this.up('window');
                var warnGrid = _that.lookupReference('ddsEdPGrid');
                var store = warnGrid.getStore();
                var values = form.getValues();
                if (values.bgDt > values.edDt) {
                    Ext.Msg.alert('提示', '起始时间需要小于结束时间'); //这种方式非常常见的
                    return;
                }
                values.river = config.RIVER;
                Ext.Ajax.request({
                    url: config.URL + 'emergency/ddsEdP/update',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            Ext.Msg.alert('系统提示', "修改成功");
                            store.reload();
                            modal.hide();
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: values
                });
            } else {
                Ext.Msg.alert('提示', '表单填写不合法！');
            }
        };
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '修改应急方案',
                width: 530,
                layout: 'fit',
                closeAction: 'hide',
                resizable: false,
                reference: 'warnUpdataWindow',
                buttons: [{
                        text: '保存',
                        handler: saveUpdata
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            _that.lookupReference('warnUpdataForm').getForm().reset();
                            modal.hide();
                        }
                    },

                ],
                items: {
                    xtype: 'warnForm',
                    border: false,
                    reference: 'warnUpdataForm'
                }
            });
            this.getView().add(win);
        }
        win.show();
        this.lookupReference('warnUpdataForm').getForm().reset();
        // console.log('rec.data', rec.data);
        this.lookupReference('warnUpdataForm').getForm().setValues(rec.data);
        _that.getViewModel().setData({ evenCd: rec.data.evenCd });
        if (rec.data.sta > -1) {
            _that.lookupReference('editEvenCd').setDisabled(true);
            _that.lookupReference('WARN_FIELD_SJCD').setDisabled(true);
            _that.lookupReference('WARN_FIELD_YRQ').setDisabled(true);
            _that.lookupReference('WARN_FIELD_QSSJ').setDisabled(true);
            _that.lookupReference('WARN_FIELD_ZZSJ').setDisabled(true);
            _that.lookupReference('editEvenpubDt').setDisabled(true);
            _that.lookupReference('editEvenproNm').setDisabled(true);
        } else {
            _that.lookupReference('editEvenCd').setDisabled(false);
            _that.lookupReference('WARN_FIELD_SJCD').setDisabled(false);
            _that.lookupReference('WARN_FIELD_YRQ').setDisabled(false);
            _that.lookupReference('WARN_FIELD_QSSJ').setDisabled(false);
            _that.lookupReference('WARN_FIELD_ZZSJ').setDisabled(false);
            _that.lookupReference('editEvenpubDt').setDisabled(false);
            _that.lookupReference('editEvenproNm').setDisabled(false);
        }
    },

    //切换应急事件
    evenCdchange: function(combo, newValue, oldValue, eOpts) {
        if (newValue == oldValue) { return; }
        var that = this;
        var data = combo.getStore().getData().items;
        for (var i = 0; i < data.length; i++) {
            if (data[i].data.evenCd == newValue) {
                combo.nextSibling().setValue(data[i].data.hapDt);
                that.lookupReference('WARN_FIELD_QSSJ').setMaxValue(data[i].data.hapDt);
                that.lookupReference('WARN_FIELD_ZZSJ').setMinValue(data[i].data.hapDt);
            }
        }
    }
});